// pages/mycoupon/mycoupon.js
var ports = require("../../utils/ports.js")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:'',
    status: 1,
    memberBonusID: '',
    completed: false,  // 弹窗控制
    toMemberCode: ''  // 存储用户输入的内容
  },
  onLoad: function (options) {
    this.getMyMemberBonusList()
  },
  changeStatus(e) {
    var that = this
    var curStatus = parseInt(that.data.status)
    var status = parseInt(e.currentTarget.dataset.status)
    if (curStatus == status) {
      return
    }
    that.setData({
      status,
      MemberBonusListPage: [],
      MemberBonusList: []
    })
    that.getMyMemberBonusList()
  },
  //红包列表
  getMyMemberBonusList() {
    var that = this

    var req = {}
    req.status = that.data.status

    req.ListName = 'MemberBonusList'
    
    ports.ModuleBonus.getMyMemberBonusList(req, that)
  },
  onPullDownRefresh: function () {
    var that = this;
    ports.ModuleAll.PullDownRefresh(that, () => {
      that.getMyMemberBonusList();
    })
  },
  onReachBottom: function () {
    var that = this;
    ports.ModuleAll.ReachBottom('MemberBonusList', that, res => {
      that.getMyMemberBonusList();
    })
  },
  category(e) {
    this.setData({
      completed:true,
      memberBonusID:e.currentTarget.dataset.id,
      index:e.currentTarget.dataset.index
    })
  },
  // 获取用户输入的内容
  getUserInput(event) {
    const toMemberCode = event.detail.value || event.detail.text
    this.setData({toMemberCode: toMemberCode})
  },

  // 取消按钮触发事件
  cancelSelected(event) {
    this.setData({
      completed:false
    })
  },

  // 确定按钮触发事件
  successSelected(event) {
    var that = this
    let toMemberCode = that.data.toMemberCode
    let memberBonusID = that.data.memberBonusID
    if(!memberBonusID){
      wx.showToast({
        title: '红包不存在',
        icon:'none',
        duration:1000
      })
      return
    }
    if(!toMemberCode){
      wx.showToast({
        title: '好友编号不能为空',
        icon:'none',
        duration:1000
      })
      return
    }
    var req = {}
    req.toMemberCode = toMemberCode
    req.memberBonusID = memberBonusID
    wx.showModal({
      title: '提示',
      content: '您真的要赠送给' + toMemberCode + '吗？',
      success(res) {
        if (res.confirm) {
          wx.showToast({
            title: '赠送中',
            icon: 'none',
            mask: true
          })
          ports.ModuleBonus.sendMemberBonusToMember(req, that, res => {
            if (res.data.header.code == 0) {
              wx.showToast({
                title: '赠送成功',
                icon: 'none'
              })
              var MemberBonusList = that.data.MemberBonusList
              MemberBonusList.splice(parseInt(that.data.index), 1)
              that.setData({
                MemberBonusList,
                completed:false
              })
            } else {
              wx.showToast({
                title: res.data.header.msg,
                icon: 'none'
              })
            }
          })
        }
      }
    })
  }
})